package com.sisgesbyte.dao.administracion;

import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.sisgesbyte.dao.beanDao.BoletinRequest;
import com.sisgesbyte.dao.beanDao.FiltrosSQLBean;
import com.sisgesbyte.dao.utilDao.CreaFiltroSqlString;
import com.sisgesbyte.data.entity.administracion.BoletinOfertaEntity;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoBoletinDaoImpl extends AbstractFacade<BoletinOfertaEntity> implements MantenimientoBoletinDao {

	@Inject
	private CreaFiltroSqlString creaFiltroSqlString;

	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}

	public MantenimientoBoletinDaoImpl() {
		super(BoletinOfertaEntity.class);
	}

	@Override
	public List<BoletinOfertaEntity> listaRegistradosBoletin(FiltrosSQLBean cadenaFiltros) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT nombre, apellido, telefono, email, fecha_registro, active, id FROM administracion.usuarios_boletin ");
		// concatenamos el filtro
		if (cadenaFiltros != null) {
			StringBuilder filtro = new StringBuilder();
			filtro.append(creaFiltroSqlString.concatenar(cadenaFiltros));
			jpql.append(filtro.toString());
		}
		Query query = em.createNativeQuery(jpql.toString(), BoletinOfertaEntity.class);
		if (cadenaFiltros != null && cadenaFiltros.getListaValores() != null
				&& cadenaFiltros.getListaValores().size() > 0) {
			int num = 0;
			for (Object valor : cadenaFiltros.getListaValores()) {
				query.setParameter(++num, valor);
			}
		}
		List<BoletinOfertaEntity> ls = query.getResultList();

		return ls;
	}

	@Override
	public GenericEntity guardarUsuarioBoletin(BoletinRequest request) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT *  from administracion.sp_registrar_usuario_boletin(:in_nombre, :in_apellido, :in_telefono, :in_email, :in_active)");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_nombre", request.getNombre());
		query.setParameter("in_apellido", request.getApellido());
		query.setParameter("in_telefono", request.getTelefono());
		query.setParameter("in_email", request.getEmail());
		query.setParameter("in_active", request.getActive());
		
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarUsuarioBoletin(BoletinRequest request) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from administracion.sp_actualizar_usuario_boletin(:in_id , :in_nombre, :in_apellido, :in_telefono, :in_email, :in_active)");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_id", request.getId());
		query.setParameter("in_nombre", request.getNombre());
		query.setParameter("in_apellido", request.getApellido());
		query.setParameter("in_telefono", request.getTelefono());
		query.setParameter("in_email", request.getEmail());
		query.setParameter("in_active", request.getActive());
		
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarUsuarioBoletin(int codRegistro) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from administracion.sp_eliminar_usuario_boletin(:in_id)");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_id", codRegistro);

		return (GenericEntity) query.getSingleResult();
	}
}
